/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddDialog.java
 *
 * Created on 3-Feb-2009, 4:57:01 PM
 */
package penny.downloadmanager.view.add;

import penny.downloadmanager.control.AddDialogControl;
import java.awt.Frame;
import java.beans.PropertyChangeEvent;
import javax.swing.JDialog;
import penny.downloadmanager.model.gui.AddDialogModel;
import java.beans.PropertyChangeListener;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author john
 */
public class AddDialog extends JDialog implements PropertyChangeListener {

    private AddURL addUrl;
    private AddList addList;
    private AddQuery addQuery;
    private AddDialogModel addModel;
    private ComboBoxModel comboModel;

    /** Creates new form AddDialog */
    public AddDialog(AddDialogModel addModel) {
        this.setLocationByPlatform(true);
        addUrl = new AddURL(addModel);
        addList = new AddList(addModel);
        addQuery = new AddQuery(addModel);
        this.addModel = addModel;
        comboModel = new DefaultComboBoxModel(AddDialogModel.PanelList.values());
        this.addModel.addPropertyChangeListener(this);
        this.setVisible(addModel.isVisible());
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jPanel1 = new javax.swing.JPanel();
        addDialogModel1 = addModel;
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        addScroller = new javax.swing.JScrollPane();
        addCombo = new javax.swing.JComboBox();

        jPanel1.setName("jPanel1"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setTitle("Add Download"); // NOI18N
        setName("Form"); // NOI18N

        jButton2.setText("Cancel"); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jButton3.setText("OK"); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        addScroller.setViewportView(addUrl);

        addCombo.setModel(comboModel);
        addCombo.setName("addCombo"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, addDialogModel1, org.jdesktop.beansbinding.ELProperty.create("${selectedPanel}"), addCombo, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(addScroller, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
                    .addComponent(addCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(addCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addScroller, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Add", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 532, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton2, jButton3});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox addCombo;
    private penny.downloadmanager.model.gui.AddDialogModel addDialogModel1;
    private javax.swing.JScrollPane addScroller;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    public void registerController(AddDialogControl addController) {
        jButton2.setActionCommand(AddDialogControl.COM_CANCEL);
        jButton2.addActionListener(addController);
        jButton3.setActionCommand(AddDialogControl.COM_OK);
        jButton3.addActionListener(addController);

        addList.registerController(addController);

    }

    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(AddDialogModel.PROP_VISIBLE)) {
            this.setVisible((Boolean) evt.getNewValue());
        } else if (evt.getPropertyName().equals(AddDialogModel.PROP_SELECTEDPANEL)) {
            if (evt.getNewValue().equals(AddDialogModel.PanelList.URL)) {
                addScroller.setViewportView(addUrl);
            } else if (evt.getNewValue().equals(AddDialogModel.PanelList.LIST)) {
                addScroller.setViewportView(addList);
            } else if (evt.getNewValue().equals(AddDialogModel.PanelList.QUERY)) {
                addScroller.setViewportView(addQuery);
            }
        }
    }
}
